<?php

namespace XWAM\Controller;

/**
 * ProductGoods 控制器类
 *
 * @since 1.0 <2016-5-28> SoChishun <14507247@qq.com> Added.
 */
class ProductGoods extends AppbaseController {

    protected $content_codes = array(array('id' => 'description', 'text' => '商品描述'), array('id' => 'attr_table', 'text' => '商品属性'), array('id' => 'after_service', 'text' => '售后保障'));

    function indexSearch() {
        $search = I('param.');
        $where = array();
        if (!empty($search['word'])) {
            $where['title|marque'] = array('like', '%' . $search['word'] . '%');
        }
        if (!empty($search['category_id'])) {
            $where['category_id'] = $search['category_id'];
        }
        if (!empty($search['title'])) {
            $where['title'] = array('like', '%' . $search['title'] . '%');
        }
        //$where['pid'] = 0;
        return array('search' => $search, 'where' => $where);
    }

    function index($category_id = 0) {
        $asearch = $this->indexSearch();
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $list = $mGoods->selectPaging($page, array('where' => $asearch['where'], 'order' => 'id desc'), array('page_params' => $asearch['search']));
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $categories = $m_category->getIdTextList($this->siteId, true);
        if ($list && $categories) {
            foreach ($list as &$row) {
                $cid = $row['category_id'];
                $row['category_text'] = isset($categories[$cid]) ? $categories[$cid] : '';
            }
        }
        $category_title = $category_id && isset($categories[$category_id]) ? $categories[$category_id] : '商品管理';
        $this->assign('meta_title', $category_title);
        $this->assign('list', $list);
        $this->assign('page', $page->showBootstrap());
        $this->assign('search', $asearch['search']);
        $this->display('index_eshop');
    }

    function getItemJson($id = '', $marque = '') {
        $where = array();
        if ($id) {
            $where = array('id' => $id);
        }
        if ($marquee) {
            $where = array('marque' => $marquee);
        }
        if (!$where) {
            $this->ajaxReturn(null);
        }
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $this->ajaxReturn($mGoods->where($where)->find());
    }

    function getListJson() {
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $this->ajaxReturn($mGoods->getIdTextList($this->siteId));
    }

    function edit($id = 0, $category_id = 0) {
        $data = array();
        if ($id) {
            $mGoods = new \XWAM\Model\ProductGoodsModel();
            $data = $mGoods->find($id);
            unset($mGoods);
        }
        if (!$data) {
            $data = array('user_name' => $this->userName, 'site_id' => $this->siteId);
        }
        if ($category_id) {
            $data['category_id'] = $category_id;
        }
        $this->assign('meta_title', $id ? '编辑' . $data['title'] : '新增商品');
        $this->assign('data', $data);
        $this->assign('content_codes', $this->content_codes);
        $this->display();
    }

    function editSave() {
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $result = $mGoods->saveGoods();
        $this->dialogJump($result['status'], $result['info']);
    }

    // 2016-7-1
    function content_edit($id = 0, $code = '') {
        if (!$id || !$code) {
            $this->error('请先保存商品!');
        }
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $data = $mGoods->findContent($id, $code);
        unset($mGoods);
        $this->assign('data', $data);
        $this->assign('content_codes', $this->content_codes);
        $this->display();
    }

    function contentEditSave() {
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $result = $mGoods->saveContent();
        $this->dialogJump($result['status'], $result['info']);
    }

    /**
     * 规格商品管理
     * @param type $pid
     * @since 1.0 2016-8-24 SoChishun Added.
     */
    function product_specification_goods_list($pid = 0) {
        if (!$pid) {
            $this->error('请先添加主商品!', U('product_goods_list'));
        }
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $list = $mGoods->selectPaging($page, array('where' => array('pid' => $pid), 'order' => 'id desc'));
        // 更新主商品规格商品数量
        $mGoods->where(array('id' => $pid))->setField('specification_product_count', $page->totalRows);
        // 获取主商品数据
        $agoods = $mGoods->field('id, title, category_id')->find($pid);
        $this->assign('data', $agoods);
        $this->assign('list', $list);
        $this->assign('page', $page->showBootstrap());
        $this->display();
    }

    /**
     * 规格商品编辑页面
     * @param type $id
     * @param type $pid
     * @param type $cid
     * @since 1.0 2016-8-24 SoChishun Added.
     */
    function product_specification_goods_edit($id = 0, $pid = 0, $cid = 0) {
        if (!$pid || !$cid) {
            $this->dialogClose(false, array('error' => '参数无效!'));
        }
        if ($id) {
            $data = M('t_porg_product_goods')->find($id);
        } else {
            $data = M('t_porg_product_goods')->find($pid);
            $data['pid'] = $pid;
            unset($data['id']);
        }
        $spec_ids = M('t_porg_product_category')->where(array('id' => $cid))->getField('specification_ids');
        $m_spec = new \XWAM\Model\ProductSpecificationModel();
        $where = array('id' => array('in', $spec_ids), 'pid' => 0);
        $tree = $m_spec->select_tree(array('where' => $where));
        $this->assign('data', $data);
        $this->assign('list', $tree);
        $this->display();
    }

    function product_specification_goods_edit_save() {
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $result = $mGoods->save_specification_goods();
        $this->dialogJump($result['status'], $result['info']);
    }

    function changePromotionStatus($id, $status, $type = '') {
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $this->ajaxReturn($mGoods->changePromotionStatus($id, $status, $type));
    }

    function changeStatus($id, $status) {
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $this->ajaxReturn($mGoods->changeStatus($id, $status));
    }

    function changeSort($id, $sort) {
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $this->ajaxReturn($mGoods->changeSort($id, $sort));
    }

    function delete($id = '') {
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $result = $mGoods->deleteGoods($id);
        $this->ajaxReturn($result);
    }

    /**
     * 选择促销活动
     * @param type $id
     * @since 1.0 2016-8-30 SoChishun Added.
     */
    function promotion($id = '') {
        if (!$id) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $list = $mGoods->where(array('id' => array('in', $id)))->field('id, title, promotion_ids')->order('sort, id desc')->select();
        $mPromotion = new \XWAM\Model\ProductPromotionModel();
        $promotions = $mPromotion->getIdTextList(0);
        $this->assign('promotions', $promotions);
        $this->assign('list', $list);
        $this->display();
    }

    function promotionSave() {
        $aid = I('id');
        $data = array();
        foreach ($aid as $id) {
            $promotionIds = I('promotion_ids_' . $id);
            $data[$id] = $promotionIds ? implode(',', $promotionIds) : '';
        }
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $result = $mGoods->savePromotion($data);
        $this->dialogJump(true);
    }

    // 2017-3-20
    function category($id = '') {
        if (!$id) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $list = $mGoods->where(array('id' => array('in', $id)))->field('id, title, category_id')->order('sort, id desc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 保存类别
     * @since 1.0 <2016-3-20> SoChishun Added.
     */
    public function categorySave() {
        $aid = I('id');
        $aCategoryId = I('category_id');
        $mGoods = new \XWAM\Model\ProductGoodsModel();
        $result = $mGoods->saveCategory($aid, $aCategoryId);
        if (!$result['status']) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $this->dialogJump(true);
    }

}
